Skip to content

Conversation

@seefeldb
Copy link
Contributor

@seefeldb seefeldb commented Jul 11, 2025

This commit refactors the validateParentPath function in transaction-shim.ts to improve its performance, especially for deep or frequent path validations during storage transactions.

Optimization Details

  • Single-pass Traversal: The previous implementation redundantly checked object-ness and existence at each path level, sometimes re-traversing the same objects. The new approach streamlines the traversal by iterating through the parent path only once, directly walking down the value tree and stopping at the first missing or non-object parent.

  • Efficient Error Handling: Error construction is now more precise, and the function avoids unnecessary object checks and array slicing. The logic for setting the error's .path property is simplified, reducing overhead in error cases.

  • No Change in Behavior: All existing functionality and error semantics are preserved. The function still returns the same errors for invalid paths, but does so with less computational overhead.


Summary by cubic

Refactored the validateParentPath function to validate parent paths in a single pass, improving performance for deep or frequent storage transactions.

  • Refactors
  • Streamlined traversal to avoid redundant checks and object lookups.
  • Simplified error handling and reduced overhead without changing error behavior.

This commit refactors the `validateParentPath` function in `transaction-shim.ts` to improve its performance, especially for deep or frequent path validations during storage transactions.

### Optimization Details

- **Single-pass Traversal:**
  The previous implementation redundantly checked object-ness and existence at each path level, sometimes re-traversing the same objects. The new approach streamlines the traversal by iterating through the parent path only once, directly walking down the value tree and stopping at the first missing or non-object parent.

- **Efficient Error Handling:**
  Error construction is now more precise, and the function avoids unnecessary object checks and array slicing. The logic for setting the error's `.path` property is simplified, reducing overhead in error cases.

- **No Change in Behavior:**
  All existing functionality and error semantics are preserved. The function still returns the same errors for invalid paths, but does so with less computational overhead.
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cubic found 2 issues across 1 file. Review them in cubic.dev

React with 👍 or 👎 to teach cubic. Tag @cubic-dev-ai to give specific feedback.

nextValue = nextValue[key] = typeof Number(key) === "number" ? [] : {};
nextValue =
nextValue[key] =
(typeof Number(key) === "number" ? [] : {}) as typeof nextValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof Number(key) === "number" is always true, so the code always creates an array and never an object, corrupting the structure when key is non-numeric

Suggested change
(typeof Number(key) === "number" ? [] : {}) as typeof nextValue;
(Number.isInteger(Number(key)) ? [] : {}) as typeof nextValue;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can't be NaN

pathError.path.push(segment);
}
}
if (parentIndex > 0) pathError.path = path.slice(0, parentIndex - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-by-one in slice: omits the last valid segment, so pathError.path reports an empty or incomplete parent path

Suggested change
if (parentIndex > 0) pathError.path = path.slice(0, parentIndex - 1);
if (parentIndex > 0) pathError.path = path.slice(0, parentIndex);

@seefeldb seefeldb merged commit 24b2ab9 into main Jul 11, 2025
8 checks passed
@seefeldb seefeldb deleted the faster-validate-parent-path branch July 11, 2025 22:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants